package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.common.vo.order.PayVO;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    private String timeout = "1m";

    //在支付宝创建的应用的id
    private   String app_id = "2021000143624818";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDCBkoFfOav3oVGDxnaukvR34nSlxtOHX6Zr41JQ3Oxtuc594oCX5zsQe/KzLP1boY6XomP5z9buB9ekLJWuN1Xkq8rRa+Pcd5vsLbuyyTzak8bABJ/dBUuTavAcHR6XjGPn8Ih00k7nCYUnM7OksEcCc7++cJVsZx+s4Z1BVhEjMAGiEqIXA2FlAYAi8w2b/C0Qm5qHLhtCc0wWcbfv4ngcmnoOuE5NpDDmd98L7ZtC6v9P0o2nbhJ/RahyzX+ZGN7KFliVZNXQpdvPWfOmyVSXOahGOX4RHV2jv4ygc5SFqaPetZuR5+CFw8hik39jEutLKlsCYCTzwRfcQWTaQoVAgMBAAECggEASMvtR+PV9dRqcp/F1pAhVGwnnNC/GO7uCiGYxA0/wkTauqAKsttG2Eng+1kUiBhg2lOQD2L3HaWWd3eYwfr4aRwLBxRNS2ewyn0mFbA4e+++IGZFinZ2X/oaX8iRmzvuInIRd3l7t5bdnsw6t7zOIYP4mygAZwCNs+gNoahQtt7lC/MBKjf4ejwLe7nSlz8cmHnEdC/whxuHfKASMzBeBfNUc4VrmxfmPcdaKyqBZd3UdI5nRxaeWAAee+0o4JQK9aeWXJHqhi/3U8eq1G02JupeBdrZHxfJgGsvFI8bPvs3ecFV7NsEmQVRmVTvpZzEOTqrhRWzHWszsL57G6TDoQKBgQDe9wRQQzfW5svhiD3U3DYiAp2mIIw0/bYCDE8Hc/Ajvs+svTdc5usK8csGGZKQfM7f37NDnbkHP2Ju7KOpxu+1u55JXwzTK7jUXL17hTC4aokZz74bCzDWbS+lpysvgXkPxbpS3V9vEy3s/ahvl/xQqQ2rlZQSyd4WfSVUz2AdfQKBgQDexZMv5/MjaNYOAUqZoWZLwc71bj0qJfWTpc+v4o3Ocql92VGWvm4y5eU2f0oiV7uCGTT9fiXsY5VfIYcnyxY7xR0utrUL66vFGDx+dV8HkotMIjAwwhxZmjnm82gHpNpIBH5lXycUCqbuyRoRgoL1N5Wjxfm+AW3nVf47pjqieQKBgQDCD3tYpHioee7AGIXAi/4tsdsm6bM+uAg/Vhk+4mEDq6MztOjH2lB/ER5k68MOMSMZ5akwU8nkjf2NkDeOoJCou2i0cZ9G+QoBA8sEBFP4Xvl/Vroe7amPKC9NXeePjIAjAzT9xKG4mpfhapG8tysrxUgZrLHZfIvpRPlGDMyaPQKBgECyTom2KjWZZ7kPhPPTHNtZgNpkVp0cAxMLL6Lb/+T6BeFHsNs/mM9tVsEZ/JS2t/0ThwErlqXtUd8b5J2Y7G8VUuFTLIc2vV7zJ/Zt7dhMJepJb4dpiEQ5dyFshUTbCo2pyG92T+oRIePtQYXeSlwe9XVntJ0K2zk5DymRoCu5AoGACAstnHpTW1GRnw7B7MR40qpSb9tHbYSgSQppkD+rlpA8tHkiHLbwn5+ZNKGI4lpbnLxXZmSRjmNhjtfZ8h4/8qBLHKAeYGTat4sNz2EndmIQ7wpBEaDC7Hzf/J5aOBAFJDbAHhBBX+ey4ACb0JhMRLchpZnl9q1NPXpZydDcxzQ=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwg1gRy5I8ZIYCXggy3MjsBzXG5Ihidsqf8T6A1UksbzwUF9jI1G3ghjj22F26kgsXLtiFKGL8F9seATNxPgSE6BLI3Sj1MoRO7sOAPy3GKcbWrnnDiRHzeetn6oXkw9mvB8vUX7LU4WDeJtWRu7GN15DgKfZCGb2+ngVn6l2ULF/XG6c/XsD31EUmKkzJiA16Y74aWUNhhUKX4aLbSqGuOSJGeDpGY9AxVTM8DdhCtlh9N7/RKJuL6eGSIjjt6XEQ57iqUi42tfg32gd/oxE9fo1Tt9apMp6E4rHclpoFrsbJgRIVeNqI2J0Q5iF0jizsmf769Hpw7WxZF9yCkWKswIDAQAB";
   // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVO vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                //支付页1分钟后就不能再付款了，自动收单功能
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
